package com.wtwd.campus.attendancereceiver;


import com.wtwd.campus.utils.InjectService;
import io.netty.channel.Channel;

/**
 * @Author ldaoliang
 * @Date create in 15:44 2021/3/25
 * @Description
 */
public interface AttendanceReceiver {

    /**
     * 解析消息
     *
     * @param data
     * @return
     */
    String parseMessage(String data, Channel channel);

    /**
     * 指令完成（删除） TODO 收到某条指令的回复时，如果该指令需要存储重发，则需要删除redis缓存的指令
     *
     * @param imei
     * @param key
     */
    default void completeCommand(String imei, String key) {
        InjectService.delCommand(imei, key);
    }


    /**
     * 将16进制转换为二进制（int）
     *
     * @param hexStr
     * @return
     */
    default Integer parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        int high = Integer.parseInt(hexStr.substring(0, 1), 16);
        int low = Integer.parseInt(hexStr.substring(1, 2),
                16);
        return high * 16 + low;
    }

    /**
     * 判断考勤机上报上来的数据是否变化过
     *
     * @param data
     * @return
     */
    default boolean judgeSecurity(String data) {
        String judge = data.substring(data.length() - 4, data.length() - 2);
        String judgeContent = data.substring(2, data.length() - 4);
        Integer result = 0;
        for (int i = 0; i < judgeContent.length() / 2; i++) {
            String compare = judgeContent.substring(i * 2, i * 2 + 2);
            Integer compareInt = parseHexStr2Byte(compare);
            Integer a = result ^ compareInt;
            result = a;
        }
        Integer judgeInt = parseHexStr2Byte(judge);
        return judgeInt.equals(result) ? true : false;
    }

    /**
     * 把十六进制转成二进制
     */
    default int convertToInt(String str){
        char[] chars = str.toCharArray();
        return (int) (Math.pow(16,3)*(Integer.valueOf(String.valueOf(chars[0])))+Math.pow(16,2)*(Integer.valueOf(String.valueOf(chars[1])))+Math.pow(16,1)*(Integer.valueOf(String.valueOf(chars[2])))+Math.pow(16,0)*(Integer.valueOf(String.valueOf(chars[3]))));
    }

    /**
     * 获取考勤机的设备编号
     * @param hexStr
     * @return
     */
    default String convertProNo(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        StringBuffer proNo=new StringBuffer();
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
           proNo.append(high * 16 + low);
        }
        return proNo.toString();
    }

    /**
     * 获取考勤机序列号
     * @param data
     * @return
     */
    default String getProNo(String data) {
        if (data.length() == 16) {
            int i = data.indexOf("00");
            if(i != -1){
                data = data.substring(0, i);
            }
        }
        StringBuilder proNo = new StringBuilder();
        for (int i = 0; i < data.length() / 2; i++) {
            String str = data.substring(2 * i, 2 * i + 2);
            int asc = parseHexStr2Byte(str);
            char s = (char) asc;
            proNo.append(s);
        }
        return proNo.toString();
    }
/*
    *//**
     * 获取卡片号码
     * @param data
     * @return
     *//*
    default String getCardNo(String data) {
        int last = data.lastIndexOf("00");
        data = data.substring(last);
        StringBuilder proNo = new StringBuilder();
        for (int i = 0; i < data.length() / 2; i++) {
            String str = data.substring(2 * i, 2 * i + 2);
            int asc = parseHexStr2Byte(str);
            char s = (char) asc;
            proNo.append(s);
        }
        return proNo.toString();
    }*/
}

